<template>
  <vue-editor v-model="editorText" :editor-toolbar="customToolbar" useCustomImaageHandler @image-add="handleImageAdded"></vue-editor>
</template>
<script>
import axios from "axios";
import {VueEditor} from "vue2-editor";
export default {
  name: "Editor",
  props: {
    text: String
  },
  components: {
    VueEditor
  },
  data() {
    return {
      customToolbar: [ //自定义工具栏
        ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
        ["blockquote", "code-block"], // 引用  代码块
        [{ header: 1 }, { header: 2 }], // 1、2 级标题
        [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
        [{ script: "sub" }, { script: "super" }], // 上标/下标
        [{ indent: "-1" }, { indent: "+1" }], // 缩进
        [{ size: ["small", false, "large", "huge"] }], // 字体大小
        [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
        [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
        [{ font: [] }], // 字体种类
        [{ align: [] }], // 对齐方式
        ["clean"], // 清除文本格式
        ["link", "image", "video"] // 链接、图片、视频
      ],
      options: [{
        value: '选项1',
        label: '通知公告'
      }, {
        value: '选项2',
        label: '赛事动态'
      }],
      editorText: this.text
    };
  },
  methods: {

    //  富文本插入图片
    // change(data){
    //   this.ruleForm.text = data;
    // }
    handleImageAdded: function(file, Editor, cursorLocation, resetUploader) {
      formData.append('file', file)

      console.log("flie:",file);
      let formData = new FormData();
      formData.append("image", file);
      console.log("formData:",formData);

      axios({
        url: "/api/v1/upload",
        method: "POST",
        data: formData
      })
          .then(result => {
            const url = result.data.url; // Get url from response
            Editor.insertEmbed(cursorLocation, "image", url);
            resetUploader();
          })
          .catch(err => {
            console.log(err);
          });
    }
  }

};
</script>

<style>
</style>


